package com.ruoyi.system.controller;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ruoyi.system.domain.ClientInfo;
import com.ruoyi.system.dto.SysNoticeDto;
import com.ruoyi.system.service.PushMessageService;
import com.ruoyi.system.util.SmsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.service.ISysNoticeService;

/**
 * 公告 信息操作处理
 * 
 * @author ruoyi
 */
@Slf4j
@RestController
@RequestMapping("/notice")
public class SysNoticeController extends BaseController
{
    @Autowired
    private ISysNoticeService noticeService;

    /**
     * 获取通知公告列表
     */
    @RequiresPermissions("system:notice:list")
    @GetMapping("/list")
    public TableDataInfo list(SysNotice notice)
    {
        startPage();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }


    @Autowired
    private SmsUtil smsUtil;

    /**
     * 发送验证码
     *
     * @Author chengxin.ju
     * @Date 2020/3/25
     * @Param [smsRequest]
     **/
    @ResponseBody
    @RequestMapping(value = "/sendVerificationCode", method = RequestMethod.POST)
    public AjaxResult sendVerificationCode(@RequestBody SysNoticeDto notice){
        return smsUtil.sendVerificationCode(notice.getPhone());
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @RequiresPermissions("system:notice:query")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable Long noticeId)
    {
        return success(noticeService.selectNoticeById(noticeId));
    }

    /**
     * 新增通知公告
     */
    @RequiresPermissions("system:notice:add")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysNotice notice)
    {
        notice.setCreateBy(SecurityUtils.getUsername());
        return toAjax(noticeService.insertNotice(notice));
    }

    /**
     * 修改通知公告
     */
    @RequiresPermissions("system:notice:edit")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysNotice notice)
    {
        notice.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(noticeService.updateNotice(notice));
    }

    /**
     * 删除通知公告
     */
    @RequiresPermissions("system:notice:remove")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable Long[] noticeIds)
    {
        return toAjax(noticeService.deleteNoticeByIds(noticeIds));
    }

    @Autowired
    private PushMessageService pushMessageService;

//    @PostMapping("/clientid")
//    public Map<String, Object> add(@RequestBody ClientInfo clientInfo) {
//        pushMessageService.updateClientid(clientInfo, SecurityUtils.getToken());
//        return success();
//    }

    @ResponseBody
    @RequestMapping(value = "/clientid", method = RequestMethod.POST)
    public AjaxResult clientid(@RequestBody ClientInfo clientInfo){
        log.error("==============================================clientid:{}", JSON.toJSONString(clientInfo));
        pushMessageService.updateClientid(clientInfo, SecurityUtils.getUserId());
        return success();
    }

}
